springCloud的註冊中心eureka

一、服務端啓動

一、配置文件html

application.ymlspring

info:
  app:
    name: 註冊中心服務器
  version: 1.0.0-SNAPSHOTapi

server:
  port: 1111
  tomcat:
    max-threads: 1000
    uri-encoding: UTF-8tomcat

spring:
  profiles:
    active: dev
  application:
    name: pluto-cerberus
  http:
    encoding:
      charset: UTF-8
      force: truespringboot

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: "http://peer1:1111/eureka/,http://peer2:1111/eureka/,http://peer3:1111/eureka/"
  server:
    # 清理間隔(單位毫秒,默認是60*1000)
    eviction-interval-timer-in-ms: 5000
    enable-self-preservation: false服務器

 

啓動類:app

@EnableEurekaServer 
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}ide

@EnableEurekaServer 服務端啓動註解微服務

二、客戶端啓動

@EnableEurekaClient //啓動EnableEureka客戶端fetch

 

SpringCLoud中的「Discovery Service」有多種實現,好比:eureka, consul, zookeeper。

1,@EnableDiscoveryClient註解是基於spring-cloud-commons依賴,而且在classpath中實現; 
2,@EnableEurekaClient註解是基於spring-cloud-netflix依賴,只能爲eureka做用;

若是你的classpath中添加了eureka,則它們的做用是同樣的。

 

二、客戶端配置文件

application.yml配置

1

2

3

4

5

6

7

8

9

10

11

12

server:

  port: 8081 #8181

spring:

  application:

    name: microservice-provider-user

eureka:

  client:

    serviceUrl:

      defaultZone: http://user:123456@localhost:8761/eureka  #註冊 中心已經開啓認證

  instance:

    prefer-ip-address: true

    instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}

 

客戶端啓動註解:

@EnableFeignClients  微服務之間的調用

微服務 經過EnableFeignClients調用其餘服務的api

今天在項目上遇到一個問題,經過當前service服務要調用到其餘service服務的api接口時,可經過EnableFeignClients調用其餘服務的api,大概的步驟以下:

一、在springboot的main處加上註解@EnableFeignClients

複製代碼

1 @EnableDiscoveryClient
 2 @SpringBootApplication
 3 @EnableFeignClients
 4 public class DeploymentServiceApplication {
 5 
 6     public static void main(String[] args){
 7         SpringApplication.run(DeploymentServiceApplication.class, args);
 8     }
 9 
10 }

複製代碼

 

二、在service層上實現接口,這裏注意value能夠用serviceId代替,可是最好用value來指定要調用的服務。

      fallback是當程序錯誤的時候來回調的方法

      方法中要用@PathVariable要註解參數

1 @FeignClient(value = "hap-user-admin-service", fallback = OrganizationLabelFeignClientFallback.class)
2 public interface OrganizationLabelFeignClient {
3     @RequestMapping(value = "/v1/organizations/{id}",method = RequestMethod.GET)
4     Organization queryOrgLabel(@PathVariable(name="id") Long id);
5 }


三、編寫程序錯誤時的回調類,實現接口,在錯誤時回調

複製代碼

1 @Service
2 public class OrganizationLabelFeignClientFallback implements OrganizationLabelFeignClient {
3     @Override
4     public Organization queryOrgLabel(Long id) {
5         return null;
6     }
7 }

複製代碼


四、調用該服務

複製代碼

1 //聲明,自動封裝
2 @Autowired
3 private OrganizationLabelFeignClient organizationLabelFeignClient;
4 
5 
6 //調用
7 Organization organization = organizationLabelFeignClient.queryOrgLabel(organizationId);

複製代碼

相關文章
相關標籤/搜索