eureka服務之間呼叫(3)

有機小白菜發表於2018-07-21

配置pom.xml

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
			<version>1.2.7.RELEASE</version>
		</dependency>
        <!--表示為去註冊中心發現其他服務 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>1.2.7.RELEASE</version>
		</dependency>
		<!--表示為web工程 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>http://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>http://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

配置properties

server:
  port: 8763
spring:
  application:
    name: service-two   這是釋出至註冊中心的服務名,靠此服務名服務之間相互呼叫
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka   配置註冊中心地址

配置啟動類

/**
 * 啟動類
 * @author WangD
 *
 */
@EnableDiscoveryClient    //通過註解@EnableDiscoveryClient 表明自己是一個eurekaclient.去服務中心註冊
@EnableFeignClients(basePackages="com.eureka.service")  //去註冊中心呼叫服務掃描包範圍
@SpringBootApplication(scanBasePackages="com.eureka")
public class CloudApplication2 {
	
	/** 啟動 */
	public static void main(String[] args) {
		SpringApplication.run(CloudApplication2.class, args);
	}

}

編寫呼叫其他服務service介面

@FeignClient("service-hi") //呼叫其服務的服務名  即上一條部落格(2)
public interface SchedualService {
	
	@RequestMapping(value="/hi",method=RequestMethod.POST)
	public String gethi();
	
}

編寫controller

呼叫器service介面呼叫其他服務

@RestController
public class CloudClient {
	
	@Autowired
	private SchedualService schedualService;
	
	@RequestMapping("/testhi")
	public String cloudOne(){
		return schedualService.gethi();
	}

相關文章