Springboot2(40)[springcloud]集成Eureka

源码地址

springboot2教程系列

基本配置

1、pom中添加依赖
<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
2、添加启动代码中添加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
	public static void main(String[] args){
	    System.setProperty("spring.profiles.active","server1");
        SpringApplication app = new SpringApplication(EurekaServerApplication.class);
        app.run(args);
    }
}
3、配置文件
spring.application.name: spring-cloud-eureka
server.port: 8001
spring.profiles: server1
#eureka的client注册到server时默认是使用hostname而不是ip,需要使用ip来服务到eureka-server上,需要在eureka的client增加配置如下:
#eureka.instance.prefer-ip-address: true
eureka.instance.instance-id: 10.10.2.60:${server.port}
#表示是否将自己注册到Eureka Server,默认为true
#eureka.client.register-with-eureka: false
#表示是否从Eureka Server获取注册信息,默认为true
eureka.client.fetch-registry: false

启动工程后,访问:http://127.0.0.1:8001/ 的注册界面

集群

双节点注册中心
1、项目的配置文件application.yml
spring.application.name: spring-cloud-eureka
server.port: 8001
spring.profiles: server1
#eureka的client注册到server时默认是使用hostname而不是ip,需要使用ip来服务到eureka-server上,需要在eureka的client增加配置如下:
eureka.instance.instance-id: 10.10.2.60:${server.port}
#表示是否将自己注册到Eureka Server,默认为true
#eureka.client.register-with-eureka: false
#表示是否从Eureka Server获取注册信息,默认为true
eureka.client.fetch-registry: false
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka  多个地址可使用 , 分隔
eureka.client.serviceUrl.defaultZone: http://10.10.2.60:8002/eureka/

---
spring.application.name: spring-cloud-eureka
spring.profiles: server2
server.port: 8002
eureka.instance.instance-id: 10.10.2.60:${server.port}
eureka.client.serviceUrl.defaultZone: http://10.10.2.60:8001/eureka/
2、添加启动类EurekaServer2Application
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer2Application {
	public static void main(String[] args){
	    System.setProperty("spring.profiles.active","server2");
        SpringApplication app = new SpringApplication(EurekaServer2Application.class);
        app.run(args);
    }

}
eureka集群使用

生产中我们可能需要三台或者大于三台的注册中心来保证服务的稳定性,配置的原理其实都一样,将注册中心分别指向其它的注册中心。这里只介绍三台集群的配置情况,其实和双节点的注册中心类似,每台注册中心分别又指向其它两个节点即可,使用application.yml来配置 。

配置跟双节点配置差不多,只添加多一个节目,serviceUrl指向其它节点

服务提供与调用

服务提供
1、pom包配置
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、配置文件

添加配置到applictaion.yml

---
#生产者
spring.application.name: spring-cloud-producer
server.port: 9001
spring.profiles: producer
eureka.client.serviceUrl.defaultZone: http://127.0.0.1:8002/eureka/,http://127.0.0.1:8001/eureka/
3、启动类

启动类中添加@EnableDiscoveryClient注解 ,添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

    public static void main(String[] args){
        System.setProperty("spring.profiles.active","producer");
        SpringApplication app = new SpringApplication(ProducerApplication.class);
        app.run(args);
    }
}
4、controller
@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Value("${server.port}")
    private int port;

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge,port:"+port;
    }
}
服务调用
1、pom包配置
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、配置文件

添加配置到applictaion.yml

---
#消费者
spring.application.name: spring-cloud-consumer
spring.profiles: consumer
server.port: 9002
eureka.client.serviceUrl.defaultZone: http://127.0.0.1:8002/eureka/,http://127.0.0.1:8001/eureka/
3、启动类

启动类添加@EnableDiscoveryClient@EnableFeignClients注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

    public static void main(String[] args){
        System.setProperty("spring.profiles.active","consumer");
        SpringApplication app = new SpringApplication(ConsumerApplication.class);
        app.run(args);
    }
}
  • @EnableDiscoveryClient :启用服务注册与发现
  • @EnableFeignClients:启用feign进行远程调用
4、feign调用实现
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {

    @RequestMapping(value = "/producer/hello")
    public String hello(@RequestParam(value = "name") String name);

}
5、web层调用远程服务
@RestController
@RequestMapping("/comsumer")
public class ConsumerController {

    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name);
    }
}
测试

访问:http://127.0.0.1:9002/comsumer/hello/tom

返回:hello tom,this is first messge,port:9001

负载均衡

以上面spring-cloud-producer为例子修改 ,添加启动类Producer2Application,配置文件如下

---
#生产者2
spring.application.name: spring-cloud-producer
server.port: 9003
spring.profiles: producer2
eureka.client.serviceUrl.defaultZone: http://127.0.0.1:8002/eureka/,http://127.0.0.1:8001/eureka/

启动项目

然后在浏览器再次输入:http://127.0.0.1:9002/comsumer/hello/tom 进行测试:

第一次返回结果:hello tom,this is first messge,port:9001

第二次返回结果:hello tom,this is first messge,port:9003

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值