相信看过 每天学点SpringCloud(一):简单服务提供者消费者调用的同学都发现了,在最后消费者调用提供者的时候把提供者的地址硬编码在了代码中,这样的方式肯定是不行的,今天,我们就是要Eureka来解决这个问题
创建Eureka服务
1.我们在原先项目的基础上再新建一个项目cloud-demo-eureka
此项目的依赖为
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>cn.org.zhixiang</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-demo-eureka</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--注意此处的依赖是SpringBoot2.0以后专用的,如果您使用的SpringBoot版本低于2.0请使用spring-cloud-starter-eureka-server-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>cn.org.zhixiang</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-demo-eureka</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--注意此处的依赖是SpringBoot2.0以后专用的,如果您使用的SpringBoot版本低于2.0请使用spring-cloud-starter-eureka-server-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建application.yml
spring:
application:
name: eureka-server
#开启权限认证
security:
basic:
enabled: true
user:
name: root
password: root
server:
host: localhost
port: 8761
eureka:
client:
#此项目不作为客户端注册
register-with-eureka: false
fetch-registry: false
service-url:
#开启权限验证后Eureka地址为 用户名:密码@地址:端口号,如未开启权限验证则直接使用 地址:端口号
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port}/eureka
application:
name: eureka-server
#开启权限认证
security:
basic:
enabled: true
user:
name: root
password: root
server:
host: localhost
port: 8761
eureka:
client:
#此项目不作为客户端注册
register-with-eureka: false
fetch-registry: false
service-url:
#开启权限验证后Eureka地址为 用户名:密码@地址:端口号,如未开启权限验证则直接使用 地址:端口号
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port}/eureka
3.创建cn.org.zhixiang包,在此包下创建CloudDemoEureApplication启动类
package cn.org.zhixiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class CloudDemoEureApplication {
public static void main(String[] args) {
SpringApplication.run(CloudDemoEureApplication.class, args);
}
}
cn.org.zhixiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class CloudDemoEureApplication {
public static void main(String[] args) {
SpringApplication.run(CloudDemoEureApplication.class, args);
}
}
4.如果您开启了权限验证并且SpringBoot版本为2.0以上的话还需要一个操作,如果不是此布可以忽略
因为2.0默认开启了csrf,如果我们现在直接启动Eureka服务的话客户端是注册不上的,所以需要把csrf关闭
在cn.org.zhixiang包下新建security包,新建WebSecurityConfigurer类
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
5.在CloudDemoEureApplication启动类中启动Eureka服务,浏览器访问http://localhost:8761/eureka,输入用户名root和密码root登陆Eureka后Eureka服务创建成功。
修改服务提供者
1.在原先的cloud-demo-provider项目中添加Eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.修改application.yml,增加以下配置
eurekaServer:
host: localhost
port: 8761
user: root
password: root
eureka:
client:
#将此项目注册到Eureka服务
register-with-eureka: true
service-url:
defaultZone: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka
host: localhost
port: 8761
user: root
password: root
eureka:
client:
#将此项目注册到Eureka服务
register-with-eureka: true
service-url:
defaultZone: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka
3.在CloudDemoProviderApplication启动类中增加一个注解:@EnableEurekaClient标示此项目是Eureka客户端
4.紧接着先启动刚才创建的Eureka服务再启动此项目,当我们再次访问Eureka的时候可以发现我们的项目已经注册上了Eureka
5.因为我们此次需要玩点高大上的了,所以只有一个服务提供者肯定是不行的,我们copy一下上方的cloud-demo-provider项目,新项目命名为cloud-demo-provider-2,这个新项目需要修改的地方只有三个:
第一是pom文件中的项目id注意不要与上个项目一样,推荐直接叫做cloud-demo-provider-2.
第二是yml文件中spring.application.name应该是与上个项目都一样的,名字叫做:provider-demo。这个不做修改,只需要修改server.port,保证端口不会冲突,比如我改成了8079
第三是UserController,可以看到上个项目返回的User是硬编码的叫做张三的,这次这个项目我们把这个张三修改成李四,来区分这两个项目
修改完毕以后启动项目Eureka中应该是这样的
就是这个provider-demo的服务有两个提供者的意思
修改消费者
现在我们已经开始修改上次服务地址硬编码的问题了
1.cloud-demo-consumer项目增加Eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.启动类中增加了两个注解
@SpringBootApplication
@EnableEurekaClient
public class CloudDemoConsumerApplication {
@Bean
@LoadBalanced//开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudDemoConsumerApplication.class, args);
}
}
@EnableEurekaClient
public class CloudDemoConsumerApplication {
@Bean
@LoadBalanced//开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudDemoConsumerApplication.class, args);
}
}
3.在UserController中做一下改变
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUser/{id}")
public User getUser(@PathVariable Long id){
return restTemplate.getForObject("http://provider-demo/user/getUser/"+id,User.class);
}
}
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUser/{id}")
public User getUser(@PathVariable Long id){
return restTemplate.getForObject("http://provider-demo/user/getUser/"+id,User.class);
}
}
可以看到,我们上次使用的localhost:8078/user/getUser已经换成了provider-demo/user/getUser了。不知道大家还记不记得这个provider-demo么?它呢,是两个项目中spring.application.name的属性值,在Eureka中呢,就对应着一个服务的id,也就是说在Eureka中,我们可以不使用ip+端口而是使用Ip去访问服务。
大家将这四个项目都启动起来以后,访问http://localhost:8088/user/getUser/5,是不是发现一会会返回张三,一会会返回李四呢。这个就是我们一开始加的@LoadBalanced注解,也就是开启了Eureka的负载均衡。这样的话我们是不是已经完美的完成了昨天遗留的问题呢?
GitHub:https://github.com/2388386839/spring-cloud-demo
码云:https://gitee.com/zhixiang_blog/apring-cloud-demo
如果对您有所帮助,请记得帮忙点一个star哦
本文出自https://zhixiang.org.cn/#/blog/read/3e9e73fd-bda0-4638-826b-cceb43b77b07,转载请保留。
相信看过 每天学点SpringCloud(一):简单服务提供者消费者调用的同学都发现了,在最后消费者调用提供者的时候把提供者的地址硬编码在了代码中,这样的方式肯定是不行的,今天,我们就是要Eureka来解决这个问题
创建Eureka服务
1.我们在原先项目的基础上再新建一个项目cloud-demo-eureka
此项目的依赖为
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>cn.org.zhixiang</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-demo-eureka</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--注意此处的依赖是SpringBoot2.0以后专用的,如果您使用的SpringBoot版本低于2.0请使用spring-cloud-starter-eureka-server-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>cn.org.zhixiang</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-demo-eureka</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--注意此处的依赖是SpringBoot2.0以后专用的,如果您使用的SpringBoot版本低于2.0请使用spring-cloud-starter-eureka-server-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建application.yml
spring:
application:
name: eureka-server
#开启权限认证
security:
basic:
enabled: true
user:
name: root
password: root
server:
host: localhost
port: 8761
eureka:
client:
#此项目不作为客户端注册
register-with-eureka: false
fetch-registry: false
service-url:
#开启权限验证后Eureka地址为 用户名:密码@地址:端口号,如未开启权限验证则直接使用 地址:端口号
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port}/eureka
application:
name: eureka-server
#开启权限认证
security:
basic:
enabled: true
user:
name: root
password: root
server:
host: localhost
port: 8761
eureka:
client:
#此项目不作为客户端注册
register-with-eureka: false
fetch-registry: false
service-url:
#开启权限验证后Eureka地址为 用户名:密码@地址:端口号,如未开启权限验证则直接使用 地址:端口号
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port}/eureka
3.创建cn.org.zhixiang包,在此包下创建CloudDemoEureApplication启动类
package cn.org.zhixiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class CloudDemoEureApplication {
public static void main(String[] args) {
SpringApplication.run(CloudDemoEureApplication.class, args);
}
}
cn.org.zhixiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class CloudDemoEureApplication {
public static void main(String[] args) {
SpringApplication.run(CloudDemoEureApplication.class, args);
}
}
4.如果您开启了权限验证并且SpringBoot版本为2.0以上的话还需要一个操作,如果不是此布可以忽略
因为2.0默认开启了csrf,如果我们现在直接启动Eureka服务的话客户端是注册不上的,所以需要把csrf关闭
在cn.org.zhixiang包下新建security包,新建WebSecurityConfigurer类
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
5.在CloudDemoEureApplication启动类中启动Eureka服务,浏览器访问http://localhost:8761/eureka,输入用户名root和密码root登陆Eureka后Eureka服务创建成功。
修改服务提供者
1.在原先的cloud-demo-provider项目中添加Eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.修改application.yml,增加以下配置
eurekaServer:
host: localhost
port: 8761
user: root
password: root
eureka:
client:
#将此项目注册到Eureka服务
register-with-eureka: true
service-url:
defaultZone: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka
host: localhost
port: 8761
user: root
password: root
eureka:
client:
#将此项目注册到Eureka服务
register-with-eureka: true
service-url:
defaultZone: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka
3.在CloudDemoProviderApplication启动类中增加一个注解:@EnableEurekaClient标示此项目是Eureka客户端
4.紧接着先启动刚才创建的Eureka服务再启动此项目,当我们再次访问Eureka的时候可以发现我们的项目已经注册上了Eureka
5.因为我们此次需要玩点高大上的了,所以只有一个服务提供者肯定是不行的,我们copy一下上方的cloud-demo-provider项目,新项目命名为cloud-demo-provider-2,这个新项目需要修改的地方只有三个:
第一是pom文件中的项目id注意不要与上个项目一样,推荐直接叫做cloud-demo-provider-2.
第二是yml文件中spring.application.name应该是与上个项目都一样的,名字叫做:provider-demo。这个不做修改,只需要修改server.port,保证端口不会冲突,比如我改成了8079
第三是UserController,可以看到上个项目返回的User是硬编码的叫做张三的,这次这个项目我们把这个张三修改成李四,来区分这两个项目
修改完毕以后启动项目Eureka中应该是这样的
就是这个provider-demo的服务有两个提供者的意思
修改消费者
现在我们已经开始修改上次服务地址硬编码的问题了
1.cloud-demo-consumer项目增加Eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.启动类中增加了两个注解
@SpringBootApplication
@EnableEurekaClient
public class CloudDemoConsumerApplication {
@Bean
@LoadBalanced//开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudDemoConsumerApplication.class, args);
}
}
@EnableEurekaClient
public class CloudDemoConsumerApplication {
@Bean
@LoadBalanced//开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudDemoConsumerApplication.class, args);
}
}
3.在UserController中做一下改变
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUser/{id}")
public User getUser(@PathVariable Long id){
return restTemplate.getForObject("http://provider-demo/user/getUser/"+id,User.class);
}
}
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUser/{id}")
public User getUser(@PathVariable Long id){
return restTemplate.getForObject("http://provider-demo/user/getUser/"+id,User.class);
}
}
可以看到,我们上次使用的localhost:8078/user/getUser已经换成了provider-demo/user/getUser了。不知道大家还记不记得这个provider-demo么?它呢,是两个项目中spring.application.name的属性值,在Eureka中呢,就对应着一个服务的id,也就是说在Eureka中,我们可以不使用ip+端口而是使用Ip去访问服务。
大家将这四个项目都启动起来以后,访问http://localhost:8088/user/getUser/5,是不是发现一会会返回张三,一会会返回李四呢。这个就是我们一开始加的@LoadBalanced注解,也就是开启了Eureka的负载均衡。这样的话我们是不是已经完美的完成了昨天遗留的问题呢?
GitHub:https://github.com/2388386839/spring-cloud-demo
码云:https://gitee.com/zhixiang_blog/spring-cloud-demo
如果对您有所帮助,请记得帮忙点一个star哦
本文出自https://zhixiang.org.cn,转载请保留。